package gui;

import collector.Chunk;
import collector.FileObj;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * @author dotekien
 *         Date: Apr 13, 2006
 *         Time: 1:16:28 PM
 */
public class SameComboBox extends JComboBox {

    public String getToolTip(Object item) {
        String textTooltip;
        if (item instanceof FileObj) {
            textTooltip = ((FileObj) item).getPath();
        } else if (item instanceof Chunk) {
            textTooltip = ((Chunk) item).getFile().getPath();
        } else {
            textTooltip = item.toString();
        }
        return textTooltip;
    }

    void setup(String toolTip, ActionListener act) {
        setBorder(BorderFactory.createEmptyBorder());
        setToolTipText(toolTip);
        setRenderer(new SameComboBoxRenderer(this));
        setOpaque(false);
        setEditable(true);
        addActionListener(act);
        Dimension dim = new Dimension(200, 17);
        setPreferredSize(dim);
        setMaximumSize(dim);
        setMinimumSize(dim);
    }

    private static class SameComboBoxRenderer extends BasicComboBoxRenderer {
        SameComboBox combox;

        public SameComboBoxRenderer(SameComboBox jcb) {
            combox = jcb;
        }

        public Component getListCellRendererComponent(JList list, Object value,
                                                      int index, boolean isSelected, boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (isSelected && value != null) {
                String textTooltip = combox.getToolTip(value);
                list.setToolTipText(textTooltip);
                //combox.setToolTipText(textTooltip);
            }
            return c;
        }
    }
}
